package com.canaan.business.controller.pc.quality;

import java.util.ArrayList;
import java.util.List;

import com.canaan.business.common.enums.quality.QualityProcessModeEnum;
import com.canaan.business.common.enums.quality.QualityResultEnum;
import com.canaan.business.common.enums.quality.QualityTaskStatusEnum;
import com.canaan.business.common.enums.quality.QualityTypeEnum;
import com.canaan.business.domain.inOutStock.WmsPickingRecord;
import com.canaan.business.domain.quality.OqcQualityMainTask;
import com.canaan.common.exception.ServiceException;
import com.canaan.common.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.canaan.common.annotation.Log;
import com.canaan.common.core.controller.BaseController;
import com.canaan.common.core.domain.AjaxResult;
import com.canaan.common.enums.BusinessType;
import javax.annotation.Resource;
import com.canaan.business.domain.quality.ProductQualityMainTask;
import com.canaan.business.service.quality.IProductQualityMainTaskService;
import com.canaan.common.utils.poi.ExcelUtil;
import com.canaan.common.core.page.TableDataInfo;

/**
 * 物料质检项Controller
 *
 * @author 
 * @date 2023-02-07
 */
@Api(tags = "物料质检项")
@RestController
@RequestMapping("/quality/qualityMainTask")
public class ProductQualityMainTaskController extends BaseController {
    @Resource
    private IProductQualityMainTaskService productQualityMainTaskService;

    /**
     * 查询物料质检项列表
     */
    @ApiOperation(value = "查询物料质检项列表")
    @GetMapping("/list")
    public TableDataInfo list(ProductQualityMainTask productQualityMainTask) {
        startPage();
        List<ProductQualityMainTask> list = productQualityMainTaskService.selectProductQualityMainTaskList(productQualityMainTask);
        return getDataTable(list);
    }

    /**
     * 导出物料质检任务列表
     */
    @ApiOperation(value = "导出物料质检任务列表")
    @Log(title = "导出物料质检任务列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProductQualityMainTask productQualityMainTask) {
        List<ProductQualityMainTask> list = productQualityMainTaskService.selectProductQualityMainTaskList(productQualityMainTask);
        // 转换状态、检验结果、处理方式为中文
        for (ProductQualityMainTask mainTask : list) {
            if (StringUtils.isNotEmpty(mainTask.getStatus())){
                mainTask.setStatus(QualityTaskStatusEnum.getMessageByCode(mainTask.getStatus()));
            }
            if (StringUtils.isNotEmpty(mainTask.getQualityResult())){
                mainTask.setQualityResult(QualityResultEnum.getMessageByCode(mainTask.getQualityResult()));
            }
            if (StringUtils.isNotEmpty(mainTask.getProcessMode())){
                mainTask.setProcessMode(QualityProcessModeEnum.getMessageByCode(mainTask.getProcessMode()));
            }
        }
        String sheetName = "来料检管理";
        if(productQualityMainTask.getQualityType().equals(QualityTypeEnum.OQC.getCode())){
            sheetName = "出货检管理";
        }
        ExcelUtil<ProductQualityMainTask> util = new ExcelUtil<ProductQualityMainTask>(ProductQualityMainTask.class);
        util.exportExcel(response, list, sheetName);
    }

    /**
     * 导出出货检任务列表
     */
    @ApiOperation(value = "导出出货检任务列表")
    @Log(title = "导出出货检任务列表", businessType = BusinessType.EXPORT)
    @PostMapping("/exportOqc")
    public void exportOqc(HttpServletResponse response, ProductQualityMainTask productQualityMainTask) {
        List<ProductQualityMainTask> list = productQualityMainTaskService.selectProductQualityMainTaskList(productQualityMainTask);
        List<OqcQualityMainTask> exportList = new ArrayList<>();
        // 转换状态、检验结果、处理方式为中文
        for (ProductQualityMainTask mainTask : list) {
            if (StringUtils.isNotEmpty(mainTask.getStatus())){
                mainTask.setStatus(QualityTaskStatusEnum.getMessageByCode(mainTask.getStatus()));
            }
            if (StringUtils.isNotEmpty(mainTask.getQualityResult())){
                mainTask.setQualityResult(QualityResultEnum.getMessageByCode(mainTask.getQualityResult()));
            }
            if (StringUtils.isNotEmpty(mainTask.getProcessMode())){
                mainTask.setProcessMode(QualityProcessModeEnum.getMessageByCode(mainTask.getProcessMode()));
            }
            OqcQualityMainTask exportEntity = new OqcQualityMainTask();
            BeanUtils.copyProperties(mainTask, exportEntity);
            exportList.add(exportEntity);
        }
        ExcelUtil<OqcQualityMainTask> util = new ExcelUtil<OqcQualityMainTask>(OqcQualityMainTask.class);
        util.exportExcel(response, exportList, "出货检管理");
    }

    /**
     * 获取物料质检项详细信息
     */
    @ApiOperation(value = "获取物料质检项详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(productQualityMainTaskService.selectProductQualityMainTaskById(id));
    }

    /**
     * 新增物料质检项
     */
    @ApiOperation(value = "新增物料质检项")
    @Log(title = "物料质检项", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductQualityMainTask productQualityMainTask) {
        return toAjax(productQualityMainTaskService.insertProductQualityMainTask(productQualityMainTask));
    }

    /**
     * 修改物料质检项
     */
    @ApiOperation(value = "修改物料质检项")
    @Log(title = "物料质检项", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductQualityMainTask productQualityMainTask) {
        return toAjax(productQualityMainTaskService.updateProductQualityMainTask(productQualityMainTask));
    }


    /**
     * 查询物料质检明细不合格数量
     */
    @ApiOperation(value = "查询物料质检明细不合格数量")
    @GetMapping("/checkTaskNgNum")
    public AjaxResult checkTaskNgNum(String taskNo) {
        return success(productQualityMainTaskService.checkTaskNgNum(taskNo));
    }

    /**
     * 完成物料质检任务
     */
    @ApiOperation(value = "完成物料质检任务")
    @Log(title = "物料质检项", businessType = BusinessType.UPDATE)
    @PutMapping("/complete")
    public AjaxResult complete(@RequestBody ProductQualityMainTask productQualityMainTask) {
        return toAjax(productQualityMainTaskService.complete(productQualityMainTask));
    }

    /**
     * 删除物料质检项
     */
    @ApiOperation(value = "删除物料质检项")
    @Log(title = "物料质检项", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(productQualityMainTaskService.deleteProductQualityMainTaskByIds(ids));
    }

    /**
     * 查询条码是否属于质检任务对应拣货单
     */
    @ApiOperation(value = "查询条码是否属于质检任务对应拣货单")
    @GetMapping("/checkTaskBarcode")
    public AjaxResult checkTaskBarcode(String taskNo,String barcode) {
        WmsPickingRecord check = productQualityMainTaskService.checkTaskBarcode(taskNo,barcode);
        if (check == null) {
            throw new ServiceException("请输入正确的条码");
        }
        return success(check);
    }
}
